<template>
  <view class="outterBox">
    <scroll-view :scroll-left="scrollLeft" scroll-x class="frame" scroll-with-animation="true">
      <view class="innerBox">
        <view
          :id="'_tab_'+index"
          :class="{ active: index === activeIndex }"
          @click="onClick(index)"
          class="label"
          v-for="(item, index) in DataList"
          :key="index"
          >{{ item.label }}</view
        >
        <view
          class="slideBox"
          :style="{ transform: 'translateX(' + slider.left + 'px)',
                    width:defaultConfig.underLineWidth+'px',
                    height:defaultConfig.underLineHeight+'px',
                    backgroundColor:defaultConfig.underLineColor
           }"
        >
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  name: "myTabs",
  props: {
    config: {
      type: Object,
      default: () => {
        return {};
      },
    },
    DataList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    deFaultIndex: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      tabList:[],
      activeIndex: -1,
      slider: {
        left: 15,
      },
      defaultConfig:{
        underLineWidth:24,
        underLineHeight:2,
        underLineColor:'#f94d2a',
        textColor:'#333333',
        activeTextColor:'#f94d2a'
      },
      scrollLeft:0
    };
  },
  methods: {
    updateTabWidth(){
      if(this.tabList.length === 0) return
      // console.log(this.tabList);
      const query = uni.createSelectorQuery().in(this)
      this.tabList.forEach((item,index) => {
        query.select(`#_tab_${index}`).boundingClientRect((res)=>{
          if(res){
            // console.log(res);
            item._slider = {
              left: res.left + (res.width-this.defaultConfig.underLineWidth)/2
            }
            // console.log(item.slider.left);
            if(this.tabList.length-1===index){
              this.changeTab()
            }
          }
        }).exec()
      });
      // console.log(this.tabList);
    },
    onClick(index) {
      this.activeIndex = index;
      
      // console.log(index);
      this.changeTab()
      this.$emit("tabClick", index);
    },
    changeTab(){
      // console.log(this.tabList);
      if(this.tabList.length===0) return
      const index = this.activeIndex
      this.slider.left = this.tabList[index]._slider.left
      this.scrollLeft = index*this.defaultConfig.underLineWidth + 'px'
    }
  },
  watch: {
    config:{
      handler(val){
        this.defaultConfig = {...this.defaultConfig,...val}
      },
      immediate:true
    },
    deFaultIndex: {
      handler(val) {
        setTimeout(()=>{
          this.activeIndex = val;
          this.changeTab()
        },0)
        
        
      },
      immediate: true,
    },
    DataList:{
      handler(val) {
        this.tabList = val;
        this.updateTabWidth()
      },
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped>
// .active {
//   color: $uni-active;
//   border-bottom: solid 5px $uni-active;
// }
.outterBox {
  width: 100%;
  height: 100%;
  .frame {
    width: 100%;
    height: 35px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    background-color: $uni-white;
    .innerBox {
      width: 100%;
      height: 100%;
      position: relative;
      .label {
        display: inline-block;
        height: 25px;
        line-height: 35px;
        text-align: center;
        font-size: $uni-font-size-base;
        padding: 0 15px;
        color: #3f3f3f;
        &.active {
          color: $uni-active;
          // border-bottom: solid 5px $uni-active;
        }
      }
      .slideBox {
        height: 2px;
        width: 25px;
        position: absolute;
        bottom: 0;
        border-radius: 2px;
        background-color: $uni-active;
        transition: 0.3s all;
        z-index: 10;
      }
    }
  }
}
</style>
